package com.wdcloud.framework_business.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wdcloud.framework_business.utils.CustomUtil;
import com.wdcloud.framework_business.wight.BaseHeadView;
import com.wdcloud.framework_business.wight.CustomNetErrorView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

/**
 * Info:
 * Created by Umbrella.
 * CreateTime: 2021/3/4 16:11
 */
public class BaseBusinessFragment extends Fragment {
    private Activity mContext;
    private boolean isIgnoreNet=false;
    private BaseHeadView customHeaderView;
    private LinearLayout mRoot;
    private boolean isNetworkConnected = true;
    /**
     * 标识Fragment是不是第一次显示
     */
    public boolean isFirstShow = true;

    protected boolean isActive;
    protected boolean mHidden = false;
    /**
     * 显示的layout布局
     */
    protected View mContainer;
    private CustomNetErrorView customNetErrorView;

    @Override
    public void onAttach(@NonNull Context mActivity) {
        super.onAttach(mActivity);
        mContext = (Activity) mActivity;
    }
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        assert inflater != null;
        return onCreateViewWithTitleBar(inflater, container, getLayout());
    }
    protected View onCreateViewWithTitleBar(LayoutInflater inflater, ViewGroup container, int resource) {
        mRoot = new LinearLayout(getActivity());
        mRoot.setOrientation(LinearLayout.VERTICAL);
        customHeaderView = new BaseHeadView(mContext);
        mRoot.addView(customHeaderView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mContainer = inflater.inflate(resource, container, false);
        ButterKnife.bind(this, mContainer);
        mRoot.addView(mContainer, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        customHeaderView.setVisibility(View.GONE);
        return mRoot;
    }
    @Override
    public void onResume() {
        super.onResume();

        if (!isIgnoreCheckNet() && !CustomUtil.isNetworkConnected(getContext())) {
            return;
        }
        loadData();
    }

    private void loadData() {
        isActive = true;
        //1、是第一次显示 2、或者不是第一次显示，但两次显示的是同一个页面。
        // 这两种情况，都要重新加载数据，目的是为了和onHiddenChanged 配合使用。
//        isFirstShow = isFirstShow || MainActivity.currentFragment == this;
        isFirstShow = !isHidden();
        if (isFirstShow) {
            visibleRefresh();
        }
        //增加了Fragment是否可见的判断
        if (!isHidden()) {
            lazyInit();
        }
    }
    /**
     * 懒加载初始化方法,只在fragment可见时加载一次,切换fragment可见时不会调用
     * 网络请求逻辑可以在fragment可见后在这里处理
     */
    public void lazyInit() {
    }
    /**
     * 当前fragment页面可见时就刷新,不限次数
     */
    public void visibleRefresh() {
    }
    /**
     * 是否忽略网络检查
     * true 直接初始化数据
     *
     * @return 默认false
     */
    protected boolean isIgnoreCheckNet() {
        return isIgnoreNet;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initTitleView();
        checkNetStateInitData();
    }

    protected int getLayout() {
        return 0;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        isActive = false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    /**
     * 当 Fragment 隐藏的状态发生改变时，该函数将会被调用，如果当前 Fragment 隐藏，
     * hidden 的值为 true, 反之为 false。最为重要的是hidden 的值，可以通过调用 isHidden() 函数获取。
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mHidden = hidden;
        isFirstShow = !hidden;
    }
    /**
     * 初始化TitleHeader
     */
    protected void initTitleView() {

    }
    /**
     * 有网络正常逻辑,没有网络显示无网络UI
     */
    private void checkNetStateInitData() {

        /** 页面让网络检查并且没有网络时候--显示无网络页面*/
        if (!isIgnoreCheckNet() && !CustomUtil.isNetworkConnected(getContext())) {//无网络状态
            isNetworkConnected = false;
            mContainer.setVisibility(View.GONE);
            if (customNetErrorView == null) {
                customNetErrorView = new CustomNetErrorView(getContext());
                mRoot.addView(customNetErrorView);
            }
        } else {//有网络状态
            mContainer.setVisibility(View.VISIBLE);
            initTitleView();
            getExtraData(getArguments());
            initViews(getArguments());

            //从无网络状态切换到有网络状态，Resume方法中的逻辑需再次执行
            if (!isNetworkConnected) {
                isNetworkConnected = true;
                loadData();
            }

        }
    }
    protected void getExtraData(Bundle bundle) {
    }
    protected void initViews(Bundle bundle) {
    }
}